package com.ge.modules.api.controller;

import java.util.Date;

import com.ge.common.annotation.LoginUserApi;
import com.ge.common.utils.DateUtils;
import com.ge.common.utils.R;
import com.ge.modules.api.apiDTO.TbUserCardDto;
import com.ge.modules.api.service.TbUserCardService;
import com.ge.modules.entity.TbUser;
import com.ge.modules.entity.TbUserCardEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户会员卡表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-23 11:40:31
 */
@RestController
@RequestMapping("api/user/usercard")
public class TbUserCardController {

    @Autowired
    private TbUserCardService userCardService;

    /**
     * 信息
     *
     * @param tbUser
     * @return
     */
    @GetMapping("/info")
    public R info(@LoginUserApi TbUser tbUser) {
        TbUserCardEntity tbUserCardEntity = new TbUserCardEntity();
        tbUserCardEntity.setUserId(tbUser.getUserId());
        TbUserCardEntity userCard = userCardService.queryByObject(tbUserCardEntity);
        if (userCard == null) {
            return R.error("没有会员卡");
        }
        TbUserCardDto tbUserCardDto = userCard.toDto();
        tbUserCardDto.setUserName(tbUser.getNickname());
        tbUserCardDto.setHeadImgUrl(tbUser.getHeadPicture());
        if (tbUserCardDto.getMonthCardStartTime() != null && tbUserCardDto.getMonthCardEndTime() != null) {
            //如果过期了 就显示0天
            Boolean bo = DateUtils.date_compare_date(tbUserCardDto.getMonthCardEndTime(), new Date());
            if(bo){
                Long haveMonthDays = DateUtils.getDistanceDays(DateUtils.dateToString(tbUserCardDto.getMonthCardStartTime()), DateUtils.dateToString(tbUserCardDto.getMonthCardEndTime()));
                tbUserCardDto.setHaveMonthDays(haveMonthDays.intValue());
            }else{
                tbUserCardDto.setHaveMonthDays(0);
            }
        }
        if (tbUserCardDto.getYearCardStartTime() != null && tbUserCardDto.getYearCardEndTime() != null) {
            //如果过期了 就显示0天
            Boolean bo = DateUtils.date_compare_date(tbUserCardDto.getYearCardEndTime(), new Date());
            if(bo) {
                Long haveYearDays = DateUtils.getDistanceDays(DateUtils.dateToString(tbUserCardDto.getYearCardStartTime()), DateUtils.dateToString(tbUserCardDto.getYearCardEndTime()));
                tbUserCardDto.setHaveYearDays(haveYearDays.intValue());
            }else{
                tbUserCardDto.setHaveYearDays(0);
            }
        }
        return R.ok().put("data", tbUserCardDto);
    }

    /**
     * 成为用户会员
     *
     * @param tbUser
     * @return
     */
    @PostMapping("/save")
    public R save(@LoginUserApi TbUser tbUser) {
        TbUserCardEntity userCard = new TbUserCardEntity();
        userCard.setCardType(0);
        userCard.setUserId(tbUser.getUserId());
        userCard.setCreateTime(new Date());
        userCardService.save(userCard);
        return R.ok();
    }


}
